﻿

var flightPath_cap_quang;
var flightPlanCoordinates_curently = [];
var point_cap_quang;
var clat_cap_quang;
var long_cap_quan;
var shape_cap_quang;
var str_cap_quang;
var sarrr_cap_quang = [];
var flightPlanCoordinates = [];
var markers_cap_quan_load_view = [];
var _encode_line_main_map_string_edit;
var _all_info_coordinate_Info_capquang_main_map_edit = [];
var _coordinate_Info_line_main_map_edit = '';

function VIEW_CAP_QUANG_ON_MAPS() {

    $(".Maps_pageInDex").mask("Dữ liệu đang được xử lý ...");

    var Stringloaicap = "";

    var valLoploaicap = $('#my_selct_loai_cap_find_vnpt_site').combotree('getValues');

    if (valLoploaicap.length == 0) {
        Stringloaicap = "";
    }
    else {
        Stringloaicap = valLoploaicap.join(",");
    }

    if (Stringloaicap == "") {

        $.confirm({
            text: "Vui lòng chọn loại cáp",
            confirm: function (button) {
            },
            cancel: function (button) {

            }
        });

        $(".Maps_pageInDex").unmask();

        return false;

    }
    else {

        setTimeout(function () {

            var path_lengh_edit = [];
            var polylineLength_edit = 0;

            $.ajax({ url: _nameSubDomain + "/TCapquang/LoadOnmapsView_CapQuang_loai_cap?loaiCap=" + Stringloaicap, async: false }).done(function (data) {

                
                var cap_quang = eval(data);

                for (xy_cap_quang = 0; xy_cap_quang < cap_quang.length; xy_cap_quang++) {

                    try {


                        shape_cap_quang = cap_quang[xy_cap_quang]["_cap_quang_line_lat_lng"].toString().split('/');

                        var bounds = new google.maps.LatLngBounds();

                        for (yzcq = 0; yzcq < shape_cap_quang.length - 1; yzcq++) {

                            str_cap_quang = shape_cap_quang[yzcq].toString();
                            sarrr_cap_quang = str_cap_quang.split(',');

                            clat_cap_quang = parseFloat(sarrr_cap_quang[0]);
                            long_cap_quang = parseFloat(sarrr_cap_quang[1]);

                            point_cap_quang = new google.maps.LatLng(clat_cap_quang, long_cap_quang);
                            bounds.extend(point_cap_quang);

                            flightPlanCoordinates.push(point_cap_quang);
                            flightPlanCoordinates_curently.push(point_cap_quang);
                        }

                        var lineSymbol = {
                            path: 'M 0,-1 0,1',
                            strokeOpacity: 1,
                            scale: 4
                        };


                        if (cap_quang[xy_cap_quang]["lapdat"] == "N") {
                            flightPath_cap_quang = new google.maps.Polyline({
                                path: flightPlanCoordinates_curently,
                                strokeColor: "#" + cap_quang[xy_cap_quang]["color"].toString(),
                                strokeOpacity: 0,
                                icons: [{
                                    icon: lineSymbol,
                                    offset: '0',
                                    repeat: '20px'
                                }],
                                map: _mapsMain,
                                editable: true,
                                draggable: true
                            });
                        }
                        else {
                            flightPath_cap_quang = new google.maps.Polyline({
                                path: flightPlanCoordinates_curently,
                                strokeColor: "#" + cap_quang[xy_cap_quang]["color"].toString(),
                                strokeOpacity: 1.0,
                                strokeWeight: 5,
                                map: _mapsMain,
                                editable: true,
                                draggable: true
                            });
                        }



                        var cneter_line = bounds.getCenter();

                        var len_lb = flightPlanCoordinates_curently.length;

                        var _line_latLng_main_map_lb_;
                        var _sum_lat_poly_lb = 0;
                        var _sum_lng_poly_lb = 0;

                        if (len_lb > 2) {
                            var int_part = parseInt(flightPlanCoordinates_curently.length / 2);
                            _sum_lat_poly_lb = flightPlanCoordinates_curently[int_part].lat();
                            _sum_lng_poly_lb = flightPlanCoordinates_curently[int_part].lng();
                        }

                        else {
                            for (var it_lb = 0; it_lb < flightPlanCoordinates_curently.length; it_lb++) {
                                if (flightPlanCoordinates_curently[it_lb] != undefined) {
                                    _sum_lat_poly_lb += flightPlanCoordinates_curently[it_lb].lat();
                                    _sum_lng_poly_lb += flightPlanCoordinates_curently[it_lb].lng();
                                }
                            }

                            _sum_lat_poly_lb = _sum_lat_poly_lb / len_lb;
                            _sum_lng_poly_lb = _sum_lng_poly_lb / len_lb;

                        }


                        markers_cap_quan_load_view.push(flightPath_cap_quang);


                        flightPlanCoordinates_curently = [];

                      

                        google.maps.event.addListener(flightPath_cap_quang, 'dblclick', (function (flightPath_cap_quang, xy_cap_quang) {
                            return function () {
                                try {
                                    //showloading(5000);
                                    //infowindow_load_view_cap_quang.close();
                                    //myInfoWindow.close();
                                    //_all_info_coordinate_Info_capquang_main_map_edit = "";
                                }
                                catch (exception) { }
                                finally {
                                    flightPath_cap_quang.setOptions({ strokeColor: '#398622', strokeWeight: 5 });
                                    _contentstring_cap_quang_mainmap = "<div id='_mainform_parent_form_save_capquang_mainmap_edit" + cap_quang[xy_cap_quang]["id"] + "'></div>";
                                    var _line_vertices_path_main_map_edit_line = flightPath_cap_quang.getPath();
                                    var _line_latLng_main_map_ = _line_vertices_path_main_map_edit_line.getAt(0);

                                    var _get_all_latlon;

                                    _get_all_latlon = _line_vertices_path_main_map_edit_line.getArray();

                                    var _sum_lat_poly = 0;
                                    var _sum_lng_poly = 0;
                                    var len = _get_all_latlon.length;

                                    if (len > 2) {
                                        var int_part = parseInt(_get_all_latlon.length / 2);
                                        _sum_lat_poly = _get_all_latlon[int_part].lat();
                                        _sum_lng_poly = _get_all_latlon[int_part].lng();
                                    }
                                    else {
                                        for (var it = 0; it < _get_all_latlon.length; it++) {
                                            if (_get_all_latlon[it] != undefined) {
                                                _sum_lat_poly += _get_all_latlon[it].lat();
                                                _sum_lng_poly += _get_all_latlon[it].lng();
                                            }
                                        }
                                        _sum_lat_poly = _sum_lat_poly / len;
                                        _sum_lng_poly = _sum_lng_poly / len;
                                    }

                                    if (_sum_lat_poly != undefined && _sum_lat_poly != 0 && _sum_lng_poly != undefined && _sum_lng_poly != 0) {
                                        _line_latLng_main_map_ = new google.maps.LatLng(_sum_lat_poly, _sum_lng_poly);
                                    }
                                    /////// End Get Location

                                    /////// Get and Encode Coordinate of Polygon
                                    for (var i = 0; i < _line_vertices_path_main_map_edit_line.length; i++) {
                                        var xy_line_main_map = _line_vertices_path_main_map_edit_line.getAt(i);
                                        _all_info_coordinate_Info_capquang_main_map_edit += xy_line_main_map.lat() + " " + xy_line_main_map.lng() + "/";

                                        var lat = parseFloat(xy_line_main_map.lat());
                                        var lng = parseFloat(xy_line_main_map.lng());
                                        var pointPath = new google.maps.LatLng(lat, lng);
                                        path_lengh_edit.push(pointPath);
                                        if (i > 0) polylineLength_edit += google.maps.geometry.spherical.computeDistanceBetween(path_lengh_edit[i], path_lengh_edit[i - 1]);

                                    }

                                    _encode_line_main_map_string_edit = google.maps.geometry.encoding.encodePath(_line_vertices_path_main_map_edit_line);
                                    if (_encode_line_main_map_string_edit) {
                                        _coordinate_Info_line_main_map_edit = '';
                                        _coordinate_Info_line_main_map_edit = _encode_line_main_map_string_edit;
                                    }


                                    //lat_long_view_load_infor_cap_quang = new google.maps.LatLng(_sum_lat_poly, _sum_lng_poly);
                                    //infowindow_load_view_cap_quang.setContent(_contentstring_cap_quang_mainmap);
                                    //infowindow_load_view_cap_quang.open(map);
                                    //infowindow_load_view_cap_quang.setPosition(_line_latLng_main_map_);
                                    //_create_form_cap_quang_edit(cap_quang[xy_cap_quang]["id"], cap_quang[xy_cap_quang], _coordinate_Info_line_main_map_edit, _all_info_coordinate_Info_capquang_main_map_edit, Math.floor(polylineLength_edit), flightPath_cap_quang);
                                }
                            }
                        })(flightPath_cap_quang, xy_cap_quang));


                    }
                    catch (e) { }

                }

                setTimeout(function () { $(".Maps_pageInDex").unmask(); }, 1000);

            });

        }, 100);
    }

}

